//
//  HYCPushDiscountViewController.m
//  FamilyTravel
//
//  Created by dllo on 15/10/23.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HYCPushDiscountViewController.h"
#import "HYCHeadDocument.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface HYCPushDiscountViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain) UICollectionView *collectionView;
@property(nonatomic, retain) NSMutableArray *arr;
@property(nonatomic, retain) MBProgressHUD *hud;
@property(nonatomic, assign)NSInteger page;
@end

@implementation HYCPushDiscountViewController
- (void)dealloc {
    [_collectionView release];
    [_arr release];
    [_hud release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.arr = [NSMutableArray array];
    self.page = 1;
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置item的带笑傲item就是tableView的row
    flowLayout.itemSize = CGSizeMake(WIDTH / 2.4, HEIGHT / 2.7);
    // 设置滑动的方向
    // 默认是垂直
    //    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 最小列间距
    flowLayout.minimumInteritemSpacing = 1;
    // 最小行间距
    flowLayout.minimumLineSpacing = 10;
    // 给item设置边距
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectionView];
    [self.collectionView release];
    // 默认背景是黑色
    self.collectionView.backgroundColor = [UIColor whiteColor];
    // 签订代理人
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    // collectioView想要创建cell的话只能使用注册的方式
    [self.collectionView registerClass:[HYCPushDiscountCell class] forCellWithReuseIdentifier:@"reuse"];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"加载中...";
    
    [self createData];

    // 上拉刷新下拉加载
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(HeaderAction)];
    
    self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.page++;
        [self createData];
        
    }];
    
}

// 刷新
-(void)HeaderAction{
    [self.arr removeAllObjects];
    [self createData];
}

- (void)createData{
    
    NSString *urlOne = [NSString stringWithFormat:@"http://open.qyer.com/lastminute/app_lastminute_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&continent_id=0&count=20&country_id=0&departure=&is_show_pay=1&is_show_supplier=1&max_id=0&page=%ld", (long)self.page];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@", urlOne, @"&page_size=20&product_type=0&times=&track_app_channel=App%2520Store&track_app_version=6.7&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=AB5B28ED-8670-4190-873A-F09EFE3EB3E8&track_os=ios%25207.1.2&v=1"];
    
    
    NSURL *urll = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urll];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        for (NSDictionary *tempDic in dic[@"data"][@"lastminutes"]) {
            HYCPushDiscountModel *model = [[HYCPushDiscountModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.arr addObject: model];
            [model release];
        }
        [self.hud removeFromSuperview];
        [self.collectionView reloadData];
        [self.collectionView.header endRefreshing];
        [self.collectionView.footer endRefreshing];
        
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arr.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HYCPushDiscountCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    if (_arr.count != 0) {
        HYCPushDiscountModel *model = _arr[indexPath.row];
        cell.model = model;
        
        cell.layer.shadowColor = [[UIColor blackColor]CGColor];
        cell.alpha = 0;
        [UIView beginAnimations:@"rotation" context:NULL];
        [UIView setAnimationDuration:1];
        cell.alpha = 1;
        [UIView commitAnimations];
    }
    
    //    NSLog(@"%@", [self.arr[indexPath.row] myUrl]);
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.arr.count != 0) {
        HYCPushDisDetailsController *pDDVC = [[HYCPushDisDetailsController alloc] init];
        [self.navigationController pushViewController:pDDVC animated:YES];
        
        pDDVC.wStr = [self.arr[indexPath.row] myUrl];
    }
        
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
